package com.hankai.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.hankai.common.ServerResponse;
import com.hankai.dao.ShippingMapper;
import com.hankai.pojo.Shipping;
import com.hankai.service.IShippingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by hankai on 2017/6/9.
 */
@Service
public class ShippingServiceImpl implements IShippingService {
    @Resource
    private ShippingMapper shippingMapper;

    @Override
    public ServerResponse addShipping(Integer userId, Shipping shipping) {
        shipping.setUserId(userId);
        int count = shippingMapper.insert(shipping);
        if (count > 0) {
            Map map = Maps.newHashMap();
            map.put("shipingId",shipping.getId());
            return ServerResponse.createBySuccess("新建地址成功",map);
        }
        return ServerResponse.createByError("新建地址失败");
    }

    @Override
    public ServerResponse deleteShipping(Integer userId, Integer shippingId) {
        int count = shippingMapper.deleteByUserIdAndId(userId,shippingId);
        if(count > 0){
            return ServerResponse.createBySuccess("删除成功");
        }
        return ServerResponse.createByError("删除失败");
    }

    @Override
    public ServerResponse updateShipping(Integer userId, Shipping shipping) {
        shipping.setUserId(userId);
        int count = shippingMapper.updateByUserIdAndId(shipping);
        if(count>0){
            return ServerResponse.createBySuccess("修改成功");
        }
        return ServerResponse.createByError("修改失败");
    }

    @Override
    public ServerResponse searchShipping(Integer userId, Integer shippingId) {
        Shipping shipping = shippingMapper.selectByIdAndUserId(shippingId,userId);
        if(shipping!=null){
            return ServerResponse.createBySuccess(shipping);
        }
        return ServerResponse.createByError("没有地址或未知错误");
    }

    @Override
    public ServerResponse searchShippingAll(Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Shipping> shippingList = shippingMapper.selectByUserId(userId);
        PageInfo pageInfo = new PageInfo(shippingList);
        return ServerResponse.createBySuccess(pageInfo);
    }
}
